var myApp = angular.module('myApp', [ 'ngGrid', 'ui.select2' ]);
myApp.controller('tempShipmentCtrl', function($scope, $http) {
	$scope.addflag = true;
	//global variable
	$scope.selectedRows = [];
	$scope.selectedRowc = [];
	$scope.selectedRowct = [];
	var objectClient = [];
	var idss = [];
	var lid = null;
	//init page item
	$scope.init = function() {
		$('input[name = "planlDt"], input[name = "planaDt"]').datetimepicker({
			minView : "month",
			format : "yyyy-mm-dd hh:ii",
		    todayBtn : "linked",
		    language : "zh-CN",
		    forceParse : false,
		    autoclose : true,
		    todayHighlight : true
		});
		$('input[name = "orderDt"], input[name = "planaDate"]').datetimepicker({
			minView : "month",
			format : "yyyy-mm-dd",
			todayBtn : "linked",
			language : "zh-CN",
			forceParse : false,
			autoclose : true,
			todayHighlight : true
		});
		$scope.stList = [{"id":0, "lb":"在用"},{"id":1, "lb":"停用"}];
			httpPost($http, {"url":ctx + "/constractor/findListBy", "data": {"ffMap":{"st" : 0}}, "ifBlock" : true}, function(data){
				$scope.constractorList = data.dt;
			}, function(){
				toastError("初始化承运商列表失败。");
			});
			
			httpPost($http, {"url":ctx + "/driver/findAllBy", "ifBlock" : true}, function(data){
				$scope.driverList = data.dt;
			}, function(){
				toastError("初始化司机列表失败。");
			});
			
			httpPost($http, {"url":ctx + "/vehicleType/findAllBy", "ifBlock" : true}, function(data){
				$scope.vehicleTypeList = data.dt;
			}, function(){
				toastError("初始化运输工具类型列表失败。");
			});
			
			httpPost($http, {"url":ctx + "/dict/getDictByCd", "params":{"cd" : "shipmentMethod"}, "ifBlock" : true}, function(data){
				$scope.shipment_methodList = data.dt;
			}, function(){
				toastError("初始化运输方式列表失败。");
			});
	};
	$scope.init();
	
	//grid
	$scope.pagingOptions = {
		pageSizes : [ 20, 50, 100 ],
		pageSize : 20,
		currentPage : 1,
		onQueryFlag : false
	};
	$scope.$watch('pagingOptions', function(newVal, oldVal) {
		if (newVal !== oldVal/* && newVal.currentPage !== oldVal.currentPage*/) {
			$scope.query();
		}
	}, true);
	
	$scope.gridOptions = {
		data : 'dt',
		totalServerItems : 'tt',
		columnDefs : [
		{
			field : "id",
			displayName : "ID", visible : false
		},
		{
			field : "tempshipmentId",
			displayName : "tempshipmentId", visible : false
		},
		{
			field : "legs_no",
			displayName : "分段号",
			width : 160
		},
		{
			field : "clientName",
			displayName : "客户",
			width : 160
		},
		{
			field : "toAddr",
			displayName : "送货地址",
			width : 250
		},
		{
			field : "cityName",
			displayName : "目的城市",
			width : 120
		},
		{
			field : "quantity",
			displayName : "数量",
			width : 120
		},
		{
			field : "weight",
			displayName : "重量",
			width : 140
		},
		{
			field : "volume",
			displayName : "体积",
			width : 140
		},
		{
			field : "palletsum",
			displayName : "托数",
			width : 120
		},
		{
			field : "toName",
			displayName : "收货方名称",
			width : 200
		},
		{
			field : "ftranslocationName",
			displayName : "出发地",
			width : 120
		},
		{
			field : "ordertime",
			displayName : "开单时间",
			width : 160,
			cellFilter : "date:'yyyy-MM-dd HH:mm'"
		},
		{
			field : "planltime",
			displayName : "计划发车时间",
			width : 160,
			cellFilter : "date:'yyyy-MM-dd HH:mm'"
		},
		{
			field : "planatime",
			displayName : "预计到达时间",
			width : 160,
			cellFilter : "date:'yyyy-MM-dd HH:mm'"
		},
		{
			field : "descr",
			displayName : "描述",
			width : 160
		}
		],
		enablePaging : true,
		pagingOptions : $scope.pagingOptions,
		enableCellEdit : true,
		enableSorting : false,
		showSelectionCheckbox : true,
		selectedItems : $scope.selectedRows,
		plugins : [ /*new ngGridFlexibleHeightPlugin()*/ ],
		i18n: 'zh-cn', rowHeight : 21, footerRowHeight : 35,
		showFooter : true,
		afterSelectionChange : function(rowItem, event){
			if($scope.selectedRows.length > 0){
				$scope.addflag = false;
			}else{
				$scope.addflag = true;
			}
			var quantity = 0;
			var weight = 0;
			var volume = 0;
			var palletsum = 0;
			$.each($scope.selectedRows,function(i,obj){
				quantity += obj.quantity;
				weight += obj.weight;
				volume += obj.volume;
				palletsum += obj.palletsum;
			});
			
			//总件数
			$("#j span").text(Math.round(quantity * 1000) / 1000);
			//总质量
			$("#w span").text(Math.round(weight * 1000) / 1000);
			//总体积
			$("#v span").text(Math.round(volume * 1000) / 1000);
			//总单数
			$("#o span").text($scope.selectedRows.length);
			//总托数
			$("#p span").text(Math.round(palletsum * 1000) / 1000);
		}
	};
	
	$scope.query = function() {
		closeModal($("#queryFormModal"));
		var requestBody = {
			pageNb : $scope.pagingOptions.currentPage,
			pageSize : $scope.pagingOptions.pageSize,
			ffMap : $scope.queryForm
		};
		setTimeout(function() {
			if($scope.queryForm == null){
				$scope.queryForm = {"tempshipmentId" : 0 };
				requestBody.ffMap = $scope.queryForm;
			}
			httpPost($http, {"url":ctx + '/legs/findListBy1', "data" : requestBody, "ifBlock" : true}, function(data){
				$scope.dt = data.dt;
				$scope.tt = data.tt;	
				$scope.gridOptions.$gridScope.toggleSelectAll(false);
			}, function(errMsg){
				toastError("查询失败。" + errMsg);
			});
		}, 100);
	};
	//$scope.query();

	//utils
	$scope.onClearSelectedRows = function() {
		$scope.gridOptions.$gridScope.toggleSelectAll(false);
	};
	
	$scope.onOpenQueryFormModal = function() {
		openModal($("#queryFormModal"));
	};
	
	$scope.pagingOptionc = {
		pageSizes : [ 20, 50, 100 ],
		pageSize : 20,
		currentPage : 1,
		onQueryFlag : false
	};
	$scope.$watch('pagingOptionc', function(newVal, oldVal) {
		if (newVal !== oldVal/* && newVal.currentPage !== oldVal.currentPage*/) {
			$scope.queryc();
		}
	}, true);
	
	$scope.gridOptionc = {
		data : 'dc',
		totalServerItems : 'tc',
		columnDefs : [
		{
			field : "id",
			displayName : "ID", visible : false
		},
		{
			field : "cd",
			displayName : "客户代码"
		},
		{
			field : "shortName",
			displayName : "客户简称"
		}
		],
		enablePaging : true,
		pagingOptions : $scope.pagingOptionc,
		enableCellEdit : true,
		showSelectionCheckbox : true,
		selectedItems : $scope.selectedRowc,
		plugins : [ /*new ngGridFlexibleHeightPlugin()*/ ],
		i18n: 'zh-cn', rowHeight : 21, footerRowHeight : 35,
		showFooter : true,
		afterSelectionChange : function(rowItem, event){
				$('#tr').empty();
				for(var i = 0; i < $scope.selectedRowc.length; i++){
					var id = $scope.selectedRowc[i]['id'];
					if(jQuery.inArray( id, idss ) < 0){
						idss.push($scope.selectedRowc[i]['id']);
						objectClient.push($scope.selectedRowc[i]);
					}
				}
				var trs = "";
				for(var i = 0; i < objectClient.length; i++){
			        trs += "<tr><td style ='display:none;'>" + objectClient[i]['id'] + "</td><td id =" + objectClient[i]['id'] + "style ='width:20%'>" + '<a href ="#"id =' + (i) + '>X</a>' + "</td><td style ='width:40%'>"
	                + objectClient[i]['cd'] + "</td><td style='width:40%'>" + objectClient[i]['shortName'] + "</td></tr>";
				}
				$("#tr").append(trs);
				$("#tr a").click(function(){
					 $(this).parent().parent().remove();	
					 idss.splice(jQuery.inArray($(this).parent().attr('id'),idss),1);
					 objectClient.splice(parseInt($(this).attr('id')),1); 
				});
		}
	};
	$scope.queryc = function() {
		var requestBody = {
			pageNb : $scope.pagingOptionc.currentPage,
			pageSize : $scope.pagingOptionc.pageSize,
			ffMap : $scope.queryFormc
		};
		setTimeout(function() {
			httpPost($http, {"url":ctx + '/client/findListBy', "data" : requestBody, "ifBlock" : true}, function(data){
				$scope.dc = data.dt;
				$scope.tc = data.tt;	
				$scope.gridOptionc.$gridScope.toggleSelectAll(false);
			}, function(errMsg){
				toastError("查询失败。" + errMsg);
			});
		}, 100);
	};
	
	
	//查询客户信息
	$scope.onOpenEditFormModalC = function(){
		$scope.onQueryc();
		openModal($("#editFormModalC"));
	};
	$scope.onQueryc = function(){
		$scope.pagingOptionc.onQueryFlag = !$scope.pagingOptionc.onQueryFlag;
		$scope.pagingOptionc.currentPage = 1;
	};
	$scope.addClient=function(){
		var shortNames = [];
		var ids = [];
		closeModal($("#editFormModalC"));
		for(var i = 0; i < objectClient.length; i++){
			shortNames.push(objectClient[i]['shortName']);
			ids.push(objectClient[i]['id']);
		}
		$("input[name = 'clientId']").val(shortNames);
		$scope.queryForm.clientId = ids;
	};
	
	$("#cl").click(function(){
		$scope.addClient();
	});
	//城市列表
	$scope.pagingOptionct = {
			pageSizes : [ 20, 50, 100 ],
			pageSize : 20,
			currentPage : 1,
			onQueryFlag : false
		};
		$scope.$watch('pagingOptionct', function(newVal, oldVal) {
			if (newVal !== oldVal/* && newVal.currentPage !== oldVal.currentPage*/) {
				$scope.queryct();
			}
		}, true);
		
		$scope.gridOptionct = {
			data : 'dct',
			totalServerItems : 'tct',
			columnDefs : [
			{
				field : "id",
				displayName : "ID", visible : false
			},
			{
				field : "cd",
				displayName : "代码"
			},
			{
				field : "name",
				displayName : "名称"
			}
			],
			enablePaging : true,
			pagingOptions : $scope.pagingOptionct,
			enableCellEdit : true,
			showSelectionCheckbox : true,
			selectedItems : $scope.selectedRowct,
			plugins : [ /*new ngGridFlexibleHeightPlugin()*/ ],
			i18n : 'zh-cn', rowHeight : 20.2, footerRowHeight : 35,
			showFooter : true,
			afterSelectionChange : function(rowItem, event){
				if($scope.selectedRowct.length > 0){
					closeModal($("#editFormModalCt"));
					$("input[name = 'cityId']").val($scope.selectedRowct[0]['name']);	
					$scope.queryForm.cityId = $scope.selectedRowct[0]['id'];
				}
			}
		};
		$scope.queryct = function() {
			var requestBody = {
				pageNb : $scope.pagingOptionct.currentPage,
				pageSize : $scope.pagingOptionct.pageSize,
				ffMap : $scope.queryFormct
			};
			setTimeout(function() {
				httpPost($http, {"url":ctx + '/city/findListBy', "data" : requestBody, "ifBlock" : true}, function(data){
					$scope.dct = data.dt;
					$scope.tct = data.tt;	
					$scope.gridOptionct.$gridScope.toggleSelectAll(false);
				}, function(errMsg){
					toastError("查询失败。" + errMsg);
				});
			}, 100);
		};
		
		//查询城市信息
		$scope.onOpenEditFormModalCt = function(){
			$scope.onQueryct();
			openModal($("#editFormModalCt"));
		};
		$scope.onQueryct = function(){
			$scope.pagingOptionct.onQueryFlag = !$scope.pagingOptionct.onQueryFlag;
			$scope.pagingOptionct.currentPage = 1;
		};
		
		
		//创建配载号
		$scope.onOpenEditFormModal = function(type, id) {
			if(type == "add"){
				$scope.editForm = {};
				var date= (new Date());
				var now = date.getFullYear() + "-" + (parseInt(date.getMonth()) + 1) + "-" + ((date.getDate() > 9) ? date.getDate() : "0" + date.getDate()) + " " + ((date.getHours() > 9) ? date.getHours() : "0" + date.getHours()) + ":" + ((date.getMinutes() > 9) ? date.getMinutes() : "0" + date.getMinutes());
				$scope.editForm.planlDt = now;
				$scope.editForm.shipment_method = 'TRUCK';
				$scope.editForm.expense = 0.0;
				$scope.editForm.expectMoney = 0.0;
				$("#e").change(function(){
					var id = $scope.editForm.constractorId;
					httpGet($http, {"url":ctx + '/constractor/id/' + id, "ifBlock" : true}, function(data){	
						$scope.editForm.name=data.dt['name'];
						httpGet($http, {"url":ctx + '/vehicle/constractorId/' + id, "ifBlock" : true}, function(data){
							$scope.vehicleList = data.dt;				
						}, function(){
							toastError("初始化运输工具列表失败。");
						});
					});	
				});
					$("#vh").click(function(){
						var vid=$scope.editForm.vehicleId;
						httpGet($http, {"url":ctx + '/vehicle/id/' + vid, "ifBlock" : true}, function(data){
							$scope.editForm.vehicleNo = data.dt['vehicleNo'];
							$scope.editForm.code = data.dt['cd'];
							$scope.editForm.vehicleTypeId = data.dt['vehicleTypeId'];
							$scope.editForm.driverId = data.dt['driverId'];
						}, function(){
							toastError("初始化运输工具列表失败。");
						});
					});
			}
			openModal($("#editFormModal"));
		};

	$('#c > option').click(function(){
		$scope.initChange();
		$scope.queryForm = {};
		$("input[name='cityId']").val(null);
		$("input[name='clientId']").val(null);
		objectClient = [];
		$scope.onQuery = function(){
			$scope.pagingOptions.onQueryFlag = !$scope.pagingOptions.onQueryFlag;
			$scope.pagingOptions.currentPage = 1;
			$scope.queryForm = {"tempshipmentId" : 0};
		};
		$scope.onQuery();
		$(".p").text('');
		$(".j").text(0);
		$(".o").text(0);
		$(".s").text(0);
		$(".w").text(0);
		$(".v").text(0);
	});
	//按业务类型查找
		$scope.initChange=function(){
		$("#tree li").css("display","block");
		$("#tree li").remove();
		var ul=$("#tree");
		httpPost($http, {"url":ctx + '/tempShipment/findListBy',"data": {"ffMap":{"vocationType" : $("#c").attr("value")}}, "ifBlock" : true}, function(data){	
			$.each(data.dt, function(i, obj){
				var li = "";
				var id = obj.id;
				var quantity = obj.quantity;
				var weight = obj.weight;
				var volume = obj.volume;
				var palletsum = obj.palletsum;
				var points = obj.points;
				var code = obj.code;
				var constractorId = obj.constractorId;				
				if(code == '未配载'){
					li="<li id= '" + id + "' style='margin-left:-20px; margin-top:1px;'><img src='/res_oms/theme-b/imgs/e01.gif' style = 'width:19px;'/><a href= '#' style='margin-top:-16px; display:block; margin-left:20px; font-size:12px; text-decoration:none;'><span class= '" + id + "'></span></a></li>";
				}else{
					if(constractorId!=null){
						 li="<li id= '"+ id + "'><img src='/res_oms/theme-b/imgs/e03.gif' style ='width:17px;margin-right:20px;'/><a href = '#' style ='margin-top:-17px;display:block;margin-left:20px;font-size:12px;text-decoration:none;'><span class= '" + id+ "'></span><img src = '/res_oms/theme-b/imgs/e04.gif' style ='width :22px;margin-left:7px;margin-right:5px;'/></a></li>";
					}else{
						 li="<li id='"+id+"'><img src='/res_oms/theme-b/imgs/e03.gif' style='width:17px;margin-right:20px;'/><a href='#' style='margin-top:-17px;display:block;margin-left:20px;font-size:12px;text-decoration:none;'><span class='"+id+"'></span></a></li>";
					};	
				};
				ul.append(li);
				$("." + id + "").text("【" + code + "】" + points + "/" + Math.round(quantity * 1000) / 1000 + "/" + Math.round(weight * 1000) / 1000 + "/" + Math.round(volume * 1000) / 1000 + "/" + Math.round(palletsum * 1000) / 1000);
			});
			//赋值
			$(".id").text($("#tree li:first").attr("id"));
				 $("#tree li").click(function() {
					 if($(this).css("background-color") == "rgb(255, 255, 255)" || $(this).css("background-color") == "transparent"){
							$(this).css("background","#DBEAF9");
						}else{
							$(this).css("background","#ffffff");
						}
				 }); 
			//点击选中
			$("#tree li").click(function() {
			$this = $(this);
			//获取选中的id
			var sts = [];
			$("#tree li").each(function(){
				if($(this).css("background-color") == "rgb(219, 234, 249)"){
					sts.push($(this).attr("id"));
				}
			});
			if(sts[0] != $("#tree li:first").attr("id") && sts.length == 1){
				$("#btnc").attr("disabled", false); 
			}else{
				$("#btnc").attr("disabled", true); 
			}
			if(sts[0] != $("#tree li:first").attr("id") && sts.length > 0){
				$("#btnq").attr("disabled", false); 
			}else{
				$("#btnq").attr("disabled", true); 
			}
			if(sts.length != 1){
				$("#zj").attr("disabled", true); 
			}else{
				$("#zj").attr("disabled", false); 
			}
			
				//配载列表右键菜单功能
				$('#style2').contextMenu('menu', {
				      //菜单项样式
				      itemStyle: {
				        fontFamily : 'verdana',
				        backgroundColor : '#F0F0F0',
				        border: 'none',
				        padding: '4px'
				      },
				      //菜单项鼠标放在上面样式
				      itemHoverStyle: {
				        border: '1px solid #fff'
				      },
				      onShowMenu: function(e, menu) {
				    	  if(sts.length == 1){
				    		  if(sts[0] == $("#tree li:first").attr("id")){
					    		  $('#confirm,#cancel,#edit,#zjdd,#qxdd', menu).remove();
					    	  }else{
					    		  if($this.find("a").find("img").length > 0){
					    			  $('#edit,#bcpz,#pc,#zjdd,#qxdd', menu).remove();  
					    		  }else{
					    			  $('#edit,#bcpz,#pc,#zjdd,#qxdd,#confirm', menu).remove(); 
					    		  };
					    	  }
				    	  }else if(sts.length > 1){
				    			if(jQuery.inArray( $("#tree li:first").attr("id"), sts ) < 0){
				    				 $('#open,#edit,#bcpz,#pc,#zjdd,#qxdd', menu).remove();
								}else{
									$('#open,#edit,#bcpz,#pc,#zjdd,#qxdd,#cancel,#confirm', menu).remove();
								}
				    		 
				    	  }else{
								$('#open,#edit,#bcpz,#pc,#zjdd,#qxdd,#cancel,#confirm', menu).remove();
				    	  }
				        return menu;
				      }
					});
				
			//对数据配载右键菜单功能
				$('#mainDataBlocks').contextMenu('menu', {
				      //菜单项样式
				      itemStyle: {
				        fontFamily : 'verdana',
				        backgroundColor : '#F0F0F0',
				        border: 'none',
				        padding: '4px'
				      },
				      //菜单项鼠标放在上面样式
				      itemHoverStyle: {
				        border: '1px solid #fff'
				      },
				      onShowMenu: function(e, menu) {
				    	if($scope.selectedRows.length === 0){
							$('#open,#edit,#bcpz,#pc,#zjdd,#qxdd,#cancel,#confirm', menu).remove();
				        }else{
				        	if(sts.length == 1){
				        		if(sts[0] == $("#tree li:first").attr("id") && $(".id").text() == $("#tree li:first").attr("id")){
				        			$('#open,#bcpz,#pc,#zjdd,#qxdd,#cancel,#confirm', menu).remove();
				        		}else{
				        			if(sts[0] == $(".id").text()){
				        				$('#open,#bcpz,#pc,#zjdd,#cancel,#confirm', menu).remove();
				        			}else{
				        				if($(".id").text() == $("#tree li:first").attr("id")){
				        					$('#open,#bcpz,#pc,#qxdd,#cancel,#confirm', menu).remove();
				        				}else{
				        					$('#open,#bcpz,#pc,#cancel,#confirm', menu).remove();
				        				}
				        			}
				        		}
				        	}else if(sts.length == 0){
				        		$('#open,#bcpz,#pc,#cancel,#zjdd,#confirm,#qxdd', menu).remove();
				        	}else{
				        		$('#open,#bcpz,#pc,#cancel,#zjdd,#confirm', menu).remove();
				        	}
				        }
				        return menu;
				      }
					});
			var shipmentId = null;
			var tempshipmentId = null;
			if(sts.length == 1){
				shipmentId = {"tempshipmentId" : sts[0]};
				tempshipmentId = sts[0];
				$("#open").attr("disabled", false); 
			}else{
				$("#open").attr("disabled", true); 
			}
			//查询
			$scope.onQuery = function(){
				hideShadow();
				$scope.pagingOptions.onQueryFlag = !$scope.pagingOptions.onQueryFlag;
				$scope.pagingOptions.currentPage = 1;
				$scope.queryForm = shipmentId;
				if(tempshipmentId == undefined){
					tempshipmentId = null;
				}
				if(tempshipmentId != null){
					httpGet($http, {"url":ctx + '/tempShipment/id/' + tempshipmentId, "ifBlock" : true}, function(data){
						$(".id").text(data.dt['id']);
						$(".p").text(data.dt['code']);
						$(".j").text(Math.round(data.dt['quantity'] * 1000) / 1000);
						$(".o").text(data.dt['points']);
						$(".s").text(data.dt['count']);
						$(".w").text(Math.round(data.dt['weight'] * 1000) / 1000);
						$(".v").text(Math.round(data.dt['volume'] * 1000) / 1000);
						$(".t").text(Math.round(data.dt['palletsum'] * 1000) / 1000);
					});
				};
			};
			
			$scope.onFilter = function(){
				$scope.pagingOptions.onQueryFlag = !$scope.pagingOptions.onQueryFlag;
				$scope.pagingOptions.currentPage = 1;
			};
			
			//同波次自动配载
			$scope.bczdpz = function(){
				hideShadow();
				httpPost($http, {"url":ctx + '/tempShipment/bczdpz',"params":{"tempshipmentId" : $("#tree li:first").attr("id")}, "ifBlock" : true}, function(data){
					if($.inArray($('.id').text(), sts) == 0){
						tempshipmentId = null;
						$(".p").text($('.p').text());
						$(".o").text(0);
						$(".j").text(0);
						$(".w").text(0);
						$(".v").text(0);
						$(".s").text(0);
						$(".t").text(0);
					}else if($.inArray($('.id').text(), sts) == -1){
						tempshipmentId = $('.id').text();
						shipmentId = {"tempshipmentId" : tempshipmentId};
					}
					tempshipmentId = $('.id').text();
					$scope.initChange();
					$scope.onQuery();
				});
			};
			
			//同波次自动配车
			$scope.bczdpc=function(){
				hideShadow();
				httpPost($http, {"url":ctx + '/tempShipment/bczdpc', "params":{"vocation" : $("#c").attr("value")}, "ifBlock" : true}, function(data){
					if($.inArray($('.id').text(), sts) == 0){
						tempshipmentId = null;
						$(".p").text($('.p').text());
						$(".o").text(0);
						$(".j").text(0);
						$(".w").text(0);
						$(".v").text(0);
						$(".s").text(0);
						$(".t").text(0);
					}else if($.inArray($('.id').text(), sts) == -1){
						tempshipmentId = $('.id').text();
						shipmentId = {"tempshipmentId" : tempshipmentId};
					}
					tempshipmentId = $('.id').text();
					$scope.initChange();
					$scope.onQuery();
				});		
			};
			
			//创建配载
			$scope.onSaveEditForm = function(){
				hideShadow();
				closeModal($("#editFormModal"));
				var selectedRowsId = [];
				var object = [];
				$.each($scope.selectedRows, function(idx, obj){
					selectedRowsId.push(obj.id);
					object.push($scope.selectedRows[idx]);
				});	
				tempshipmentId = $scope.selectedRows[0]['tempshipmentId'];
				if($scope.editForm.planaDt != undefined){
					$scope.editForm.planaDt = ymdhms2date($scope.editForm.planaDt);
				}else{
					$scope.editForm.planaDt = null;
				}
				if($scope.editForm.planlDt != undefined){
					$scope.editForm.planlDt = ymdhms2date($scope.editForm.planlDt);		
				}else{
					$scope.editForm.planlDt = null;
				}
				httpPost($http, {"url":ctx + '/tempShipment/save',"data" : $scope.editForm, "params":{"vocationType" : $scope.selectedRows[0]['vocationType'],"tempshipmentId" : tempshipmentId,"ids" : selectedRowsId}, "ifBlock" : true}, function(data){
					toastSuccess("保存成功");
					shipmentId = {"tempshipmentId" : tempshipmentId};
					$(".j").text(Math.round(data.dt['quantity'] * 1000) / 1000);
					$(".v").text(Math.round(data.dt['volume'] * 1000) / 1000);
					$(".w").text(Math.round(data.dt['weight'] * 1000) / 1000);
					$(".t").text(Math.round(data.dt['palletsum'] * 1000) / 1000);
					$(".o").text(data.dt['points']);
					$(".s").text(data.dt['count']);
					$scope.initChange();
					$scope.onQuery();	
				}, function(errMsg){
					toastError("保存失败。" + errMsg);
				});
			};
			
			//增加订单
			$scope.zjdd = function(){
				hideShadow();
				var id = parseInt($(".id").text());
				var id1 = sts[0];
				var ids = [];
				$.each($scope.selectedRows, function(idx, obj){
					ids.push(obj.id);
				});
				if($scope.selectedRows.length < 1) { toastWarning("请至少选择一笔记录进行操作。"); return; }
				httpPost($http, {"url":ctx + '/tempShipment/zjdd', "params":{"tempshipmentId" : id1, "tempshipmentId1" : id, "legsIds" : ids}, "ifBlock" : true}, function(data){
					toastSuccess("增加成功");
					if($.inArray($('.id').text(), sts) == 0){
						tempshipmentId = null;
						$(".p").text($('.p').text());
						$(".o").text(0);
						$(".j").text(0);
						$(".w").text(0);
						$(".v").text(0);
						$(".s").text(0);
						$(".t").text(0);
					}else if($.inArray($('.id').text(), sts) == -1){
						tempshipmentId = $('.id').text();
						shipmentId = {"tempshipmentId":tempshipmentId};
					}
					$scope.initChange();
					$scope.onQuery();
				});	
			};	
			
			//取消调度
			$scope.qxdd = function(){
				hideShadow();
				var id = $("#tree li:first").attr("id");
				if(lid == null || lid != sts[0]){
					lid = parseInt($('.id').text());
				}
				var ids = [];
				$.each($scope.selectedRows, function(idx, obj){
					ids.push(obj.id);
				});
				if($scope.selectedRows.length < 1) { toastWarning("请至少选择一笔记录进行操作。"); return; }
				if(lid == id) { toastWarning("未配载数据不能进行此操作。"); return; }
				httpPost($http, {"url":ctx + '/tempShipment/qxdd', "params":{"tempshipmentId" : lid, "tempshipmentId1" : id, "legsIds" : ids}, "ifBlock" : true}, function(data){
					toastSuccess("取消成功");
					if($.inArray(lid, sts) == 0){
						tempshipmentId = null;
						$(".p").text($('.p').text());
						$(".o").text(0);
						$(".j").text(0);
						$(".w").text(0);
						$(".v").text(0);
						$(".s").text(0);
						$(".t").text(0);
					}else if($.inArray($('.id').text(), sts) == -1){
						tempshipmentId = $('.id').text();
						shipmentId = {"tempshipmentId" : tempshipmentId};
					}
					$scope.initChange();
					$scope.onQuery();
				});	
			};
			
			//取消配载
			$scope.cancel=function(){
				hideShadow();
				//获取值
				httpPost($http, {"url":ctx +'/tempShipment/findByIds', "params":{"ids" : sts}, "ifBlock" : true}, function(data){	
					if($.inArray($('.id').text(), sts) == 0){
						tempshipmentId = null;
						$(".p").text($('.p').text());
						$(".o").text(0);
						$(".j").text(0);
						$(".w").text(0);
						$(".v").text(0);
						$(".s").text(0);
						$(".t").text(0);
					}else if($.inArray($('.id').text(), sts) == -1){
						tempshipmentId = $('.id').text();
						shipmentId = {"tempshipmentId" : tempshipmentId};
					}
					sts = [];
					$scope.initChange();
					$scope.onQuery();
				});
			};	
			
				//确认配载
				$scope.confirmp = function(){
					hideShadow();
					//获取当前选择的tempshipment的值
					httpPost($http, {"url":ctx + '/tempShipment/saveByIds', "params":{"ids" : sts}, "ifBlock" : true}, function(data){	
						if($.inArray($('.id').text(), sts) == 0){
							tempshipmentId = null;
							shipmentId = {"tempshipmentId" : 0};
							$(".p").text('');
							$(".o").text(0);
							$(".j").text(0);
							$(".w").text(0);
							$(".v").text(0);
							$(".s").text(0);
							$(".t").text(0);
						}else if($.inArray($('.id').text(), sts) == -1){
							tempshipmentId = $('.id').text();
							shipmentId = {"tempshipmentId" : tempshipmentId};
						}	
						sts = [];
						$scope.initChange();
						$scope.onQuery();
						}, function(errMsg){
							toastWarning("查询此笔记录失败。");		 
						});
					};
				});
			});
		};
});